package com.example.service.impl;

import com.example.entity.User;
import com.example.entity.example.UserExample;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public User getByIdOrPhone(String idOrPhone) {
        if (idOrPhone.length() == 11) {
            UserExample example = new UserExample();
            UserExample.Criteria criteria = example.createCriteria();
            criteria.andPhoneEqualTo(idOrPhone);
            List<User> users = userMapper.selectByExample(example);
            return users == null || users.isEmpty() ? null : users.get(0);
        }
        int id = Integer.parseInt(idOrPhone);
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public long getCountByExample(UserExample example) {
        return userMapper.countByExample(example);
    }

    @Override
    public List<User> getByExampleWithLimit(UserExample example, Integer page, Integer limit) {
        int begin = (page - 1) * limit;
        return userMapper.selectByExampleWithLimit(example, begin, limit);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateById(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    @Transactional
    public void updateSelectiveById(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }


    @Override
    public User getByTeacherId(String id) {
        return userMapper.selectByTeacherId(id);
    }

    @Override
    @Transactional
    public void bindTeacher(Integer userId, String teacherId) {
        userMapper.bindTeacher(userId, teacherId);
    }

    @Override
    public User getByStudentId(String id) {
        return userMapper.selectByStudentId(id);
    }

    @Override
    @Transactional
    public void bindStudent(Integer userId, String studentId) {
        userMapper.bindStudent(userId, studentId);
    }

    @Override
    public boolean judgeBind(Integer id) {
        String studentId = userMapper.getStudentId(id);
        String teacherId = userMapper.getTeacherId(id);
        return studentId != null && !studentId.isEmpty() || teacherId != null && !teacherId.isEmpty();
    }

    @Override
    public String getStudentId(Integer userId) {
        return userMapper.getStudentId(userId);
    }

    @Override
    public String getTeacherId(Integer userId) {
        return userMapper.getTeacherId(userId);
    }

    @Override
    @Transactional
    public void removeById(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }
}
